package com.whw.fy.service.impl;

import com.whw.fy.entity.*;
import com.whw.fy.mapper.*;
import com.whw.fy.service.CriticalTimeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

@Service
public class CriticalTimeServiceImpl implements CriticalTimeService {


    @Resource
    private ClazzMapper clazzMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private AttendanceTeaMapper attendanceTeaMapper;
    @Resource
    private AttendanceStuMapper attendanceStuMapper;
    @Resource
    private EmergencyMapper emergencyMapper;
    @Resource
    private CommentMapper commentMapper;

    /**
     * 小程序获取关键时间
     * @param classId
     * @param skcd
     * @return
     */
    @Override
    public Message selectByClassIdAndSkcd(int classId, int skcd) {
        HashMap hashMap = new HashMap();

        /**
         * 获得教师签到情况
         */
        AttendanceTea attendanceTea = attendanceTeaMapper.selectByClassIdAndCompletionTimes(classId,skcd);
        if (attendanceTea==null)
            hashMap.put("attendanceTea",null);
        else
            hashMap.put("attendanceTea",attendanceTea);

        /**
         * 获得学生出勤统计
         */
        int studentAttendNumber=0;
        int studentSumNumber=0;
        List<AttendanceStu> attendanceStuList = attendanceStuMapper.selectByClassIDAndCompletionTimes(classId, skcd);
        for (AttendanceStu attendanceStu:attendanceStuList){
            studentSumNumber=studentSumNumber+1;
            if(attendanceStu.getAttendance() == 1){
                studentAttendNumber=studentAttendNumber+1;
            }
        }
        HashMap attendanceStuMap  = new HashMap();
        attendanceStuMap.put("studentAttendNumber",studentAttendNumber);
        attendanceStuMap.put("studentSumNumber",studentSumNumber);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss");
        attendanceStuMap.put("date",simpleDateFormat.format(attendanceStuList.get(0).getPostTime()));
        hashMap.put("attendanceStu",attendanceStuMap);

        /**
         * 获得待办事件
         */
        List<Emergency> emergencies = emergencyMapper.selectByEmergencyClazzIdAndEmergencyClazzSkcd(classId,skcd);
        for (Emergency emergency:emergencies){
            if (emergency.getEmergencyMold()==0)
                emergency.setStudent(studentMapper.selectByPrimaryKey(emergency.getEmergencyStudentId()));
        }
        hashMap.put("emergencie",emergencies);

        /**
         * 获得评价
         */
        List<Comment> commentList = commentMapper.selectByCommClassIdAndCommClassSkcd(classId,skcd);
        hashMap.put("comment",commentList);

        return Message.success(null).add(hashMap);
    }
}
